﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;

namespace Codes {
    public class SiteConfig {

        public static bool add(string pyName, string title, string inputType, string input_w_h, string desc, string group, string order) {
            string sql="insert into Configs([pyName],[title],[inputType],[input_w_h],[desc],[group],[order]) values(@pyName,@title,@inputType,@input_w_h,@desc,@group,@order)";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                new OleDbParameter("@pyName", pyName),
                new OleDbParameter("@title", title),
                new OleDbParameter("@inputType", inputType),
                new OleDbParameter("@input_w_h", input_w_h),
                new OleDbParameter("@desc", desc),
                new OleDbParameter("@group", group),
                new OleDbParameter("@order", order),
            })>0;
        }
        public static bool update(string pyName, string title, string inputType, string input_w_h, string desc, string group, string order, string id) {
            string sql="update Configs set pyName=@pyName,title=@title,inputType=@inputType,input_w_h=@input_w_h,[desc]=@desc,[group]=@group,[order]=@order where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                new OleDbParameter("@pyName", pyName),
                new OleDbParameter("@title", title),
                new OleDbParameter("@inputType", inputType),
                new OleDbParameter("@input_w_h", input_w_h),
                new OleDbParameter("@desc", desc),
                new OleDbParameter("@group", group),
                new OleDbParameter("@order", order),
                new OleDbParameter("@ID", id)
            })>0;
        }
        public static bool changeOrder(int Order, int ID) {
            string sql="update Configs set [order]=@Order where ID=@ID";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                new OleDbParameter("@order", Order),
                new OleDbParameter("@ID", ID)
            })>0;
        }

        public static bool SaveConfig(System.Collections.Hashtable ht) {
            int j=0;
            foreach(System.Collections.DictionaryEntry de in ht) {
                string sql="update Configs set [content]=@content where ID=@ID;";
                int ret= Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql, new OleDbParameter[]{
                    new OleDbParameter("@content", de.Value),
                    new OleDbParameter("@ID", de.Key)
                });
                if(ret>0) { j++; }
            }
            return j==ht.Count;
        }

        public static bool deleteMass(string idList) {
            string sql="delete from Configs where ID in("+idList+")";
            return Codes.AccessHelper.ExecuteNonQuery(connectionString.Manager, sql)>0;
        }
        public static System.Data.DataTable getList(int pageindex, int pagesize) {
            int notTop=((pageindex-1)*pagesize);
            string sql=string.Empty;
            if(notTop==0) {
                sql="select top "+pagesize+" * from Configs order by [order] asc";
            } else {
                sql ="select top "+pagesize+" * from Configs where id not in(select top "+((pageindex-1)*pagesize)+" id from Configs) order by [order] asc";
            }
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Manager, sql).Tables[0];
        }

        public static System.Data.DataTable getList(string group) {
            string sql="select * from Configs where [group]='"+group+"' order by [order] asc";
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Manager, sql).Tables[0];
        }

        public static System.Data.DataTable getInfo(string id, string colms="*") {
            string sql="select "+colms+" from Configs where [id]="+id;
            return Codes.AccessHelper.ExecuteDataSet(connectionString.Manager, sql).Tables[0];
        }
    }
}